package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import uoc.biblioteca.mappings.object.FondoVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class FondoVo.
 * @see uoc.biblioteca.mappings.dao.FondoVo
 * @author Hibernate Tools
 */
public class FondoHome extends BaseHome {

	private static final Log log = LogFactory.getLog(FondoHome.class);

	private final SessionFactory sessionFactory = null;


	public FondoVo findById(java.lang.Integer id) {
		log.debug("getting FondoVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			FondoVo instance = (FondoVo) session.get("uoc.biblioteca.mappings.object.FondoVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
				
			}
			
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void saveNew(FondoVo instance) {
		
		log.debug("save new FondoVo instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	//Mostrar todos los proveedores
	public FondoVo[] getAllFondo() {
		
		/* will hold the books we are going to return later */
		try{
			List fondos = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.FondoVo as c order by c.nombreFondo ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				FondoVo fondoTemp = (FondoVo) iter.next();
				fondos.add(fondoTemp);
			}
			tx.commit();
			
			return (FondoVo[]) fondos.toArray(new FondoVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//Obtener el proveedor por codigo
	public FondoVo findFondoByCode(String code) {
		
		/* will hold the books we are going to return later */
		try{
			FondoVo fondo = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.FondoVo as c where c.codeFondo = '"+code+"'  ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				fondo= (FondoVo) iter.next();
				
			}
			tx.commit();
			
			return fondo;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Obtener el proveedor por descripcion en ajax
	public FondoVo findFondoByDesc(String desc) {
		
		/* will hold the books we are going to return later */
		try{
			FondoVo fondo = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.FondoVo as c where c.nombreFondo = '"+desc+"'  ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				fondo= (FondoVo) iter.next();
				
			}
			tx.commit();
			
			return fondo;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Mostrar todos los proveedores
	public List<String> getAllFondoForAjax() {
		
		/* will hold the books we are going to return later */
		try{
			List<String> fondos = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.FondoVo as c order by c.nombreFondo ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				FondoVo fondoTemp = (FondoVo) iter.next();
				fondos.add(fondoTemp.getNombreFondo());
			}
			tx.commit();
			
			return fondos;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
